/**
 * 
 */
package com.wsj.web.action.homeSiteNews;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.service.IHomeSiteNewsTitleNewsServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.web.action.BaseAction;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.HomeSiteNews.AdminNewsTitleNewsForm;
import com.wsj.web.vo.homeSite.HomeSiteNewsTitleNews;

/**
 * @author fengbin
 *  首页标题新闻管理
 */
public class AdminNewsTitleNewsAction extends BaseAction 
{
	private IHomeSiteNewsTitleNewsServ  homeSiteNewsTitleNewsServ;
	
	/*****
	 * 添加主标题新闻,
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage = "homeSiteNewsTitleNewsAdd";
		AdminNewsTitleNewsForm  adminNewsTitleNewsForm = (AdminNewsTitleNewsForm)form;
		HomeSiteNewsTitleNews  homeSiteNewsTitleNews = new HomeSiteNewsTitleNews();
		if(this.checkForm(request,adminNewsTitleNewsForm))
		{
			BeanUtils.copyProperties(homeSiteNewsTitleNews, adminNewsTitleNewsForm);
			homeSiteNewsTitleNews.setAddTime(new Date());
			//homeSiteNewsTitleNews.setParenId(null);
			
			if(homeSiteNewsTitleNews.getParenId()==null || homeSiteNewsTitleNews.getParenId()==0)
			{
				homeSiteNewsTitleNews.setParenId(null);
			}
			
			Boolean isok=this.getHomeSiteNewsTitleNewsServ().addHomeSiteNewsTitleNews(homeSiteNewsTitleNews);
			if(isok)
			{
				this.setPopMsg(request, "msg", "添加标题新闻成功!");
				
				//清除form
				AdminNewsTitleNewsForm adminNewsTitleNewsForm_bak = new AdminNewsTitleNewsForm();
				request.setAttribute("adminNewsTitleNewsForm", adminNewsTitleNewsForm_bak);
			}
			else
			{
				this.setPopMsg(request, "msg", "添加标题新闻失败!");
			}
		}
		
		return mapping.findForward(toPage);
		
		
		
	}
	
	/*****
	 * 到 添加 副标题
	 */
	public  ActionForward toAddSubhead(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String id=request.getParameter("id");
		AdminNewsTitleNewsForm  adminNewsTitleNewsForm = new AdminNewsTitleNewsForm();
		
		HomeSiteNewsTitleNews homeSiteNewsTitleNews  = this.getHomeSiteNewsTitleNewsServ().loadById(Integer.valueOf(id));
		
		adminNewsTitleNewsForm.setParenId(Integer.valueOf(id));
		adminNewsTitleNewsForm.setTitleType(new Short("1"));
		request.setAttribute("adminNewsTitleNewsForm", adminNewsTitleNewsForm);
		request.setAttribute("addSubhead","addSubhead");
		request.setAttribute("parentName", homeSiteNewsTitleNews.getTitleName());
		return mapping.findForward("homeSiteNewsTitleNewsAdd");
	}
	
	/*******
	 * 添加 副标题
	 */
	public ActionForward addSubhead(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage = "homeSiteNewsTitleNewsSubheadAdd";
		AdminNewsTitleNewsForm  adminNewsTitleNewsForm = new AdminNewsTitleNewsForm();
		
		HomeSiteNewsTitleNews  homeSiteNewsTitleNews = new HomeSiteNewsTitleNews();
		if(this.checkForm(request,adminNewsTitleNewsForm))
		{
			BeanUtils.copyProperties(homeSiteNewsTitleNews, adminNewsTitleNewsForm);
			homeSiteNewsTitleNews.setAddTime(new Date());
			homeSiteNewsTitleNews.setParenId(adminNewsTitleNewsForm.getParenId());
			Boolean isok=this.getHomeSiteNewsTitleNewsServ().addHomeSiteNewsTitleNews(homeSiteNewsTitleNews);
			if(isok)
			{
				this.setPopMsg(request, "msg", "添加标题新闻成功!");
				
				//清除form
				AdminNewsTitleNewsForm adminNewsTitleNewsForm_bak = new AdminNewsTitleNewsForm();
				request.setAttribute("adminNewsTitleNewsForm", adminNewsTitleNewsForm_bak);
			}
			else
			{
				this.setPopMsg(request, "msg", "添加标题新闻失败!");
			}
		}
		
		return mapping.findForward(toPage);
	}
	
	
	
	/********
	 * 编辑标题新闻
	 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage="homeSiteNewsTitleNewsUpdate";
	   String id=request.getParameter("id");
	   if(id!=null  && !id.equals(""))
	   {
		   HomeSiteNewsTitleNews  homeSiteNewsTitleNews = this.getHomeSiteNewsTitleNewsServ().loadById(Integer.valueOf(id));		   
		   AdminNewsTitleNewsForm  adminNewsTitleNewsForm = new AdminNewsTitleNewsForm();
		   BeanUtils.copyProperties(adminNewsTitleNewsForm, homeSiteNewsTitleNews);
		   request.setAttribute("adminNewsTitleNewsForm",adminNewsTitleNewsForm);
	   }
	   
	   return mapping.findForward(toPage);
	}
	
	
	/*******
	 * 更新标题新闻
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage="homeSiteNewsTitleNewsList";
		AdminNewsTitleNewsForm  adminNewsTitleNewsForm =(AdminNewsTitleNewsForm)form;
		
		if(adminNewsTitleNewsForm.getId()!=null)
		{
			HomeSiteNewsTitleNews  homeSiteNewsTitleNews = this.getHomeSiteNewsTitleNewsServ().loadById(adminNewsTitleNewsForm.getId());
			homeSiteNewsTitleNews.setTitleName(adminNewsTitleNewsForm.getTitleName());
			homeSiteNewsTitleNews.setTitleType(adminNewsTitleNewsForm.getTitleType());
			homeSiteNewsTitleNews.setLink(adminNewsTitleNewsForm.getLink());
			homeSiteNewsTitleNews.setParenId(adminNewsTitleNewsForm.getParenId());
			
			boolean isok=this.getHomeSiteNewsTitleNewsServ().update(homeSiteNewsTitleNews);
			if(isok)
			{
				this.setPopMsg(request, "msg", "更新标题新闻成功!");
			}
			else
			{
				this.setPopMsg(request, "msg", "更新标题新闻失败!");
			}
		}
		
		this.loadBackList(request);
		
		return mapping.findForward(toPage);
		
	}
	
	
	/*****
	 * 搜索
	 */
	public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage="homeSiteNewsTitleNewsList";
		
		this.loadResultSet(request);
		
		return mapping.findForward(toPage);
		
	}
	

	/******
	 * 加载参数,并且查询出结果
	 * @param request
	 */
	private void loadResultSet(HttpServletRequest request) 
	{
		try {
			String keyWordStr=request.getParameter("keyWord");
			String parentIdStr=request.getParameter("parenId");
			String titleTypeStr=request.getParameter("titleType");
			String pageStr=request.getParameter("page");
			String pageSizeStr=request.getParameter("pageSize");
			
			Integer parentId=null;
			Short titleType=null;
			Integer page=PageContent.pageValue;
			Integer pageSize=PageContent.pageSizeValue;
			
			AdminNewsTitleNewsForm adminNewsTitleNewsForm  = new AdminNewsTitleNewsForm(); 
			
			if(keyWordStr!=null  && !keyWordStr.equals(""))
			{
				adminNewsTitleNewsForm.setKeyWord(keyWordStr);
				request.setAttribute("keyWord", keyWordStr);
			}
			if(parentIdStr!=null  && !parentIdStr.equals(""))
			{
				parentId  =Integer.valueOf(parentIdStr);
				adminNewsTitleNewsForm.setParenId(parentId);
				request.setAttribute("parentId",parentId);
			}
			
			if(titleTypeStr!=null  && !titleTypeStr.equals("") && !titleTypeStr.equals("-1"))
			{
				titleType = Short.valueOf(titleTypeStr);
				adminNewsTitleNewsForm.setTitleType(titleType);
				request.setAttribute("titleType", titleTypeStr);
			}
			
			if(pageStr!=null  && !pageStr.equals(""))
			{
				page=Integer.valueOf(pageStr);
				adminNewsTitleNewsForm.setPage(page);
			}
			
			if(pageSizeStr!=null  && !pageSizeStr.equals(""))
			{
				pageSize = Integer.valueOf(pageSizeStr);
				adminNewsTitleNewsForm.setPageSize(pageSize);
			}
			
			
			PageResultSet pageResultSet  = this.getHomeSiteNewsTitleNewsServ().loadHomeSiteNewsTitleNewsList(adminNewsTitleNewsForm);
			if(pageResultSet!=null  && pageResultSet.getResult()!=null)
			{
			  	// 设置上一页
				request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
				//下一页
				request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
				//当前页
				request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
				
				//总共页数
				request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
				//大小
				request.setAttribute(PageContent.pageSize,pageResultSet.getPageSize());
				
				request.setAttribute(PageContent.recordNum, pageResultSet.getRecordNum());
				
//				结果集
				request.setAttribute("homeSiteNewsTitleNewsList", pageResultSet.getResult());
			}
			
			
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		
		
		
		
		
	}


	/******
	 * 返回到页面列表
	 * @param request
	 */
	private void loadBackList(HttpServletRequest request) 
	{
		this.loadResultSet(request);
		
	}


	/******
	 * 删除标题新闻
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String toPage="homeSiteNewsTitleNewsList";
	    String id=request.getParameter("id");
	    if(id!=null  && !id.equals(""))
	    {
	    	HomeSiteNewsTitleNews homeSiteNewsTitleNews = this.getHomeSiteNewsTitleNewsServ().loadById(Integer.valueOf(id));  
	    	this.getHomeSiteNewsTitleNewsServ().deleteHomeSiteNewsTitleNews(homeSiteNewsTitleNews);
	    }
	    
	    //加载返回页面列表
	    this.loadBackList(request);
	    
	    return mapping.findForward(toPage);
	}
	
	
	

	/******
	 * 检查form是否条件符合
	 * @param adminNewsTitleNewsForm
	 * @return
	 */
	private boolean checkForm(HttpServletRequest request,AdminNewsTitleNewsForm adminNewsTitleNewsForm) 
	{
		boolean isok=false;
		if(adminNewsTitleNewsForm.getTitleName()!=null  && !adminNewsTitleNewsForm.getTitleName().equals(""))
		{
			isok=true;
		}
		else
		{
			this.setPopMsg(request, "msg", "添加标题新闻失败!");
		}
		
		return isok;
	}

	public IHomeSiteNewsTitleNewsServ getHomeSiteNewsTitleNewsServ() {
		return homeSiteNewsTitleNewsServ;
	}

	public void setHomeSiteNewsTitleNewsServ(
			IHomeSiteNewsTitleNewsServ homeSiteNewsTitleNewsServ) {
		this.homeSiteNewsTitleNewsServ = homeSiteNewsTitleNewsServ;
	}
	
	
	
	

}
